'''
模拟对局 并且记录所有的碰子结果
过程如下：
1.（随机）开局
2.（循环移动开始）使用basic.getAccessibility查询我方/敌方所有可移动点
3.（注意：该函数目前参数有posList和概率表，他俩都被用在判断敌方某一棋子是否为军棋、dilei上，
这里现在可以直接用cMap里的敌方棋子编号查询到类型之后判断。因此这俩参数改完之后应该删掉）
随机选点移动（选点后调用simulate.simMove）
4.使用basic.game_end判断游戏是否结束，结束了就退出循环
'''
import asses
import simulate
import chess_map
import basic
import log
import numpy as np

def choose_chess(isEne, cMap):
    """
    给定行动方， 选定一个行动序列不为空的棋子。
    :param i:棋子坐标
    :param j:
    :param isEne:是否敌方棋子
    :param cMap:
    :return:返回可以行动的棋子的坐标
    """

    '''chess_list = []
    if isEne:
        for i in range(len(cMap.ene_chess_list)):
            x, y = cMap.ene_chess_list[i]
            action_list = basic.getAccessibility(x, y, isEne, cMap)
            if len(action_list) > 0:
                chess_list.append((x, y))
        # print('ene chess list', chess_list)

    else:
        for i in range(len(cMap.my_chess_list)):
            x, y = cMap.my_chess_list[i]
            action_list = basic.getAccessibility(x, y, isEne, cMap)
            if len(action_list) > 0:
                chess_list.append((x, y))
        #  print(list)'''
    chess_list = basic.get_action_list(isEne, cMap)
    if len(chess_list) > 0:
        #  如果有可以行动的棋子
        index = np.random.randint(0, len(chess_list))
        return chess_list[index]
    else:
        #  没有可以行动的棋子
        return -1, -1


def record(x1, y1, x2, y2, cMap, isEne):
    """
    记录碰子结果
    :param x1:
    :param y1:
    :param x2:
    :param y2:
    :param cMap:
    :param isEne:
    :return:
    """
    if cMap.map[x2, y2] == 0:
        # 如果没碰子就不记录
        return 1  # 磨棋次数 + 1
    if isEne:
        #  如果是敌方行动， 那x1, y1就是敌方棋子
        number = cMap.map[x1, y1]
        usType = cMap.map[x2, y2]
        eneType = cMap.ene_map[cMap.map[x1, y1]]
        result = asses.ChessComparisons(cMap.map[x2, y2], cMap.map[x1, y1], cMap)

    else:
        #  如果是我方行动， 那x1, y1就是我方棋子
        number = cMap.map[x2, y2]
        usType = cMap.map[x1, y1]
        eneType = cMap.ene_map[cMap.map[x2, y2]]
        result = asses.ChessComparisons(cMap.map[x1, y1], cMap.map[x2, y2], cMap)
        if eneType == 12:
            #  如果敌方军旗被吃
            result = 3

    #  记录碰子结果
    new_log = log.fightLog(number, usType, eneType, result)
    return 0  # 没有磨棋 磨棋次数清零


def move_chess(isEne, cMap, move_num):
    """
    选择一个棋子 随机移动 记录碰子结果（如果有） 返回碰子后的cMap
    :param isEne:
    :param cMap:
    :param move_num: 磨棋次数
    :return:返回碰子后的cMap
    """
    x1, y1 = -1, -1
    x1, y1 = choose_chess(isEne, cMap)
    action_list = basic.getAccessibility(x1, y1, isEne, cMap)
    index = np.random.randint(0, len(action_list))
    x2, y2 = action_list[index]
    if record(x1, y1, x2, y2, cMap, isEne):
        # 没有碰子磨棋次数+1
        move_num += 1
    else:
        move_num = 0

    new_map, is_fight = simulate.simMove(cMap, x1, y1, x2, y2, isEne)
    # print('move: ',x1, y1, x2, y2)
    return move_num


def play():
    chess_map.probTable={}
    log.allLog=[]
    cMap = chess_map.chess_map()
    #print(cMap.map)
    move_num = 0
    isEne = np.random.randint(0, 2)
    while not basic.game_end(cMap, move_num):
        move_num = move_chess(isEne, cMap, move_num)
        isEne = not isEne
        '''print(cMap.map)
        for i in range(len(log.allLog)):
            print(log.allLog[i].__dict__)
        print('moqi: ', move_num)'''

    if len(log.allLog)==0:
        return play()
    else:
        return log.match(chess_map.probTable, log.allLog)


if __name__ == '__main__':
    chess_map.probTable = {}
    log.allLog = []
    cMap = chess_map.chess_map()
    # print(cMap.map)
    move_num = 0
    isEne = np.random.randint(0, 2)
    while not basic.game_end(cMap, move_num):
        move_num = move_chess(isEne, cMap, move_num)
        isEne = not isEne
    print(cMap.map)




